package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.vo.GzhUserVo;
import org.dromara.system.domain.vo.WbItemFileVo;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbGoodsListInfoBo;
import org.dromara.system.domain.vo.WbGoodsListInfoVo;
import org.dromara.system.domain.WbGoodsListInfo;
import org.dromara.system.mapper.WbGoodsListInfoMapper;
import org.dromara.system.service.IWbGoodsListInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 货物清单Service业务层处理
 *
 * @author Lion Li
 * @date 2024-04-26
 */
@RequiredArgsConstructor
@Service
public class WbGoodsListInfoServiceImpl implements IWbGoodsListInfoService {

    private final WbGoodsListInfoMapper baseMapper;

    /**
     * 查询货物清单
     */
    @Override
    public WbGoodsListInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询货物清单列表
     */
    @Override
    public TableDataInfo<WbGoodsListInfoVo> queryPageList(WbGoodsListInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbGoodsListInfo> lqw = buildQueryWrapper(bo);
        //lqw.eq(bo.getCategoryId() != null, WbItemInfo::getCategoryId, bo.getCategoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getItemId()), WbGoodsListInfo::getItemId, bo.getItemId());
        Page<WbGoodsListInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询货物清单列表
     */
    @Override
    public List<WbGoodsListInfoVo> queryList(WbGoodsListInfoBo bo) {
        LambdaQueryWrapper<WbGoodsListInfo> lqw = buildQueryWrapper(bo);
        lqw.eq(StringUtils.isNotBlank(bo.getItemId()), WbGoodsListInfo::getItemId, bo.getItemId());
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbGoodsListInfo> buildQueryWrapper(WbGoodsListInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbGoodsListInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSubStandardId()), WbGoodsListInfo::getSubStandardId, bo.getSubStandardId());
        lqw.like(StringUtils.isNotBlank(bo.getSubStandardName()), WbGoodsListInfo::getSubStandardName, bo.getSubStandardName());
        lqw.like(StringUtils.isNotBlank(bo.getBagName()), WbGoodsListInfo::getBagName, bo.getBagName());
        lqw.eq(StringUtils.isNotBlank(bo.getBagId()), WbGoodsListInfo::getBagId, bo.getBagId());
        lqw.like(StringUtils.isNotBlank(bo.getItemCompanyName()), WbGoodsListInfo::getItemCompanyName, bo.getItemCompanyName());
        lqw.like(StringUtils.isNotBlank(bo.getDemandCompanyName()), WbGoodsListInfo::getDemandCompanyName, bo.getDemandCompanyName());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), WbGoodsListInfo::getMaterialName, bo.getMaterialName());
        lqw.eq(StringUtils.isNotBlank(bo.getEngineerVoltageLevel()), WbGoodsListInfo::getEngineerVoltageLevel, bo.getEngineerVoltageLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialDescription()), WbGoodsListInfo::getMaterialDescription, bo.getMaterialDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), WbGoodsListInfo::getUnit, bo.getUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getNumber()), WbGoodsListInfo::getNumber, bo.getNumber());
        lqw.eq(bo.getFirstDeliveryDate() != null, WbGoodsListInfo::getFirstDeliveryDate, bo.getFirstDeliveryDate());
        lqw.eq(bo.getLastDeliveryDate() != null, WbGoodsListInfo::getLastDeliveryDate, bo.getLastDeliveryDate());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliveryLocation()), WbGoodsListInfo::getDeliveryLocation, bo.getDeliveryLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliveryMethod()), WbGoodsListInfo::getDeliveryMethod, bo.getDeliveryMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getTechnicalSpecificationCode()), WbGoodsListInfo::getTechnicalSpecificationCode, bo.getTechnicalSpecificationCode());
        lqw.eq(StringUtils.isNotBlank(bo.getProvincePurchaseRequisitionNumber()), WbGoodsListInfo::getProvincePurchaseRequisitionNumber, bo.getProvincePurchaseRequisitionNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadPurchaseRequisitionNumber()), WbGoodsListInfo::getHeadPurchaseRequisitionNumber, bo.getHeadPurchaseRequisitionNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), WbGoodsListInfo::getMaterialCode, bo.getMaterialCode());
        lqw.eq(StringUtils.isNotBlank(bo.getExtendDescription()), WbGoodsListInfo::getExtendDescription, bo.getExtendDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getExtendCode()), WbGoodsListInfo::getExtendCode, bo.getExtendCode());
        lqw.eq(StringUtils.isNotBlank(bo.getItemId()), WbGoodsListInfo::getItemId, bo.getItemId());
        return lqw;
    }

    /**
     * 新增货物清单
     */
    @Override
    public Boolean insertByBo(WbGoodsListInfoBo bo) {
        WbGoodsListInfo add = MapstructUtils.convert(bo, WbGoodsListInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改货物清单
     */
    @Override
    public Boolean updateByBo(WbGoodsListInfoBo bo) {
        WbGoodsListInfo update = MapstructUtils.convert(bo, WbGoodsListInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbGoodsListInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除货物清单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<WbGoodsListInfoVo> queryPageGoodsList(WbGoodsListInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbGoodsListInfo> lqw = buildQueryWrapper(bo);
        //lqw.eq(bo.getCategoryId() != null, WbItemInfo::getCategoryId, bo.getCategoryId());
        lqw.groupBy(WbGoodsListInfo::getMaterialName);
        lqw.eq(StringUtils.isNotBlank(bo.getItemId()), WbGoodsListInfo::getItemId, bo.getItemId());
        Page<WbGoodsListInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<WbGoodsListInfoVo> queryCountList(Long id) {
        return baseMapper.selectCountVoList(id);
    }


}
